package com.cefa.edu.controller;

import com.cefa.edu.common.result.FailureResult;
import com.cefa.edu.common.result.Result;
import com.cefa.edu.common.result.ReturnMessage;
import com.cefa.edu.common.result.SuccessResult;
import com.cefa.edu.model.Score;
import com.cefa.edu.model.StudentInfo;
import com.cefa.edu.service.AcademicRegisterService;
import com.google.common.collect.Maps;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("academic")
public class AcademicRegisterController {
    private final static Logger logger = LoggerFactory.getLogger(AcademicRegisterController.class);

    @Autowired
    private AcademicRegisterService academicRegisterService;

    @RequestMapping(path = "findStudentName", method = RequestMethod.GET)
    public Result<List<StudentInfo>> findStudentName(StudentInfo studentInfo) {
        try {
            List<StudentInfo> students = academicRegisterService.findStudentName(studentInfo);
            return new SuccessResult<>(new ReturnMessage("200", "success"), students);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage());
            return new FailureResult<>(new ReturnMessage("500", e.getMessage()));
        }
    }

    @RequestMapping(path = "findStudentInfo", method = RequestMethod.GET)
    @CrossOrigin
    public Result<List<StudentInfo>> findStudentInfo(Integer user_id) {
        try {
            List<StudentInfo> students = academicRegisterService.findStudentInfo(user_id);
            return new SuccessResult<>(new ReturnMessage("200", "success"), students);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage());
            return new FailureResult<>(new ReturnMessage("500", e.getMessage()));
        }
    }

    @RequestMapping(path = "searchStudent", method = RequestMethod.GET)
    @CrossOrigin
    public Result<List<StudentInfo>> searchStudent(String searchText, Integer user_id) {
        try {
            List<StudentInfo> students = academicRegisterService.searchStudent(searchText, user_id);
            return new SuccessResult<>(new ReturnMessage("200", "success"), students);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage());
            return new FailureResult<>(new ReturnMessage("500", e.getMessage()));
        }
    }

    /**
     * @param score
     * @param user_id
     * @return
     * 保存学生的考试分数
     */
    @RequestMapping(path = "saveStudentScore/{user_id}", method = RequestMethod.POST)
    @CrossOrigin
    public Result<Score> saveStudentScore(Score score, @PathVariable Integer user_id) {
        try {
            //Score studentScore = academicRegisterService.saveStudentScore(score, user_id);
            return new SuccessResult<>(new ReturnMessage("200", "success"));
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage());
            return new FailureResult<>(new ReturnMessage("500", e.getMessage()));
        }
    }

    /**
     * 电子档案查询学生
     * @param studentInfo
     * @return
     */
    @RequestMapping(path = "findStudents", method = RequestMethod.POST)
    @CrossOrigin
    public Result<Map<String,Object>> findStudents(StudentInfo studentInfo) {
        Map<String, Object> result = Maps.newHashMap();
        if(studentInfo.getUser_id()==null && studentInfo.getUser_id().toString().equals("")){
            return new SuccessResult(new ReturnMessage("200", "success"),result.put("",""));
        }
        try {
            List<StudentInfo> students = academicRegisterService.findStudentName(studentInfo);
            return new SuccessResult(new ReturnMessage("200", "success"), students);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return new FailureResult<>(new ReturnMessage("500", e.getMessage()));
        }
    }
}
